// 获取应用实例
const app = getApp()

Page({
  data: {
    username: '', //用户名
    avatarimg: '', //用户头像
    userID: '', //用户id
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName'), // 如需尝试获取用户信息可改为false
    iconList: [{
        class: 'iconfont icon-0090',
        title: '待付款',
        id: 1
      },
      {
        class: 'iconfont icon-daifahuo',
        title: '待发货',
        id: 2
      },
      {
        class: 'iconfont icon-daishouhuo',
        title: '待收货',
        id: 3
      },
      {
        class: 'iconfont icon-shoucang-',
        title: '待评价',
        id: 4
      }
    ],
    usedList: [],
    whetherlogin: true,
  },

  //   跳转到优惠券页面
  skipToCoupon() {
    wx.switchTab({
      url: '/pages/coupon/coupon',
    })
  },
  //   余额页面  //   冻结页面
  skipToCapital() {
    wx.navigateTo({
      url: '../../my-insidePage/capital/capital'
    })
  },

  // 退出登录
  outlogin() {
    var that = this
    wx.showModal({
      title: '确认退出登录吗？',
      content: '臣妾离不开你啊......',
      success(res) {
        if (res.confirm) {
          wx.removeStorage({
            key: 'token',
          })
          that.setData({
            whetherlogin: true
          })
        }
      }
    })
  },

  toLogin() {
    wx.getUserProfile({ //获取用户信息
      desc: 'getUserProfile',
      success: resProfile => {
        this.login(),
          wx.setStorage({ //存本地 用户名
            key: 'username',
            data: resProfile.userInfo.nickName
          })
        wx.setStorage({ //存本地  用户头像
          key: 'avataring',
          data: resProfile.userInfo.avatarUrl
        })
      }
    })
  },

  // 封装
  login() {
    wx.login({ //登录
      success: res => {
        if (res.code) {
          wx.request({
            url: 'https://api.it120.cc/fanmengzhuo/user/wxapp/authorize',
            method: 'POST',
            data: {
              code: res.code
            },
            header: {
              "content-type": "application/x-www-form-urlencoded"
            },
            success: res => {
              wx.setStorage({
                key: "token",
                data: res.data.data.token || ''
              })
              wx.setStorage({
                key: 'userID',
                data: res.data.data.uid
              })
              this.setData({
                whetherlogin: false
              })
            }
          })
        }
      }
    })
  },

  //   跳转到优惠券页面
  skipToCoupon() {
    wx.switchTab({
      url: '/pages/coupon/coupon',
    })
  },
  //   余额页面  //   冻结页面
  skipToCapital() {
    wx.navigateTo({
      url: '../../my-insidePage/capital/capital'
    })
  },
  //   积分页面
  skipToPoints() {
    wx.navigateTo({
      url: '../../my-insidePage/points/points'
    })
  },
  //   成长值页面
  skipToGrowth() {
    wx.navigateTo({
      url: '../../my-insidePage/growthValue/growthValue'
    })
  },
  //我的订单
  skipToMyorder(e) {
    wx.navigateTo({
      url: `../../my-insidePage/myorder/myorder?index=${e.currentTarget.dataset.indexid}`,
    })

  },
  //售后
  skipToAftersales() {
    wx.navigateTo({
      url: '/my-insidePage/after-sales/after-sales',
    })
  },
  // CPS订单
  skipToCps() {
    wx.navigateTo({
      url: '/my-insidePage/cps/cps',
    })
  },
  //回收订单
  skipToRecovery() {
    wx.navigateTo({
      url: '/my-insidePage/recovery/recovery',
    })
  },
  // 优惠买单
  skipToDiscount() {
    wx.navigateTo({
      url: '/my-insidePage/discount/discount',
    })
  },
  // 申请发票
  skipToInvoice() {
    wx.navigateTo({
      url: '/my-insidePage/invoice/invoice',
    })
  },
  // 开票记录
  skipToBilling() {
    wx.navigateTo({
      url: '/my-insidePage/billing/billing',
    })
  },
  // 签到赚积分
  skipToSignIn() {
    wx.navigateTo({
      url: '/my-insidePage/sign-in/sign-in',
    })
  },
  // 三级分销
  skipToDis() {
    wx.navigateTo({
      url: '/my-insidePage/distribution/distribution',
    })
  },
  // 积分券兑换积分
  skipToCoupons() {
    wx.navigateTo({
      url: '/my-insidePage/coupons/coupons',
    })
  },
  // 积分兑换成长值
  skipToGrow() {
    wx.navigateTo({
      url: '/my-insidePage/grow/grow',
    })
  },
  // 帮助中心
  skipToHelp() {
    wx.navigateTo({
      url: '/my-insidePage/help/help',
    })
  },
  // 个人中心
  skipToPersonal() {
    wx.navigateTo({
      url: '/my-insidePage/personal/personal',
    })
  },
  // 系统设置
  skipToSystem() {
    wx.navigateTo({
      url: '/my-insidePage/system/system',
    })
  },

  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

  // 判断是否登录
  iflogin() {
    let token = wx.getStorageSync('token') || ''
    if (token === '') {
      this.setData({
        whetherlogin: true
      })
    } else {
      this.setData({
        whetherlogin: false
      })
    }
  },

  onLoad() {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }

    this.iflogin()
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})